package generics_and_collections.lists;

/**
 * Created with IntelliJ IDEA.
 * User: Sam
 * Date: 07/02/14
 * Time: 22:13
 * To change this template use File | Settings | File Templates.
 */
public class SingleLinkedList {

	public Link firstLink;

	public SingleLinkedList() {
	}

	public boolean isEmpty() {
		return firstLink == null;
	}

	public void insertFirst(String val) {

		Link newLink = new Link(val);
		newLink.next = firstLink;
		firstLink = newLink;
	}

	public Link removeFirst() {
		Link tmpLink = firstLink;   // only so we can return Link

		if(!isEmpty()) {
			firstLink = firstLink.next;
		}

		return tmpLink;
	}

	public String toString() {

		StringBuffer sb = new StringBuffer();
		Link currentLink = firstLink;

		while(currentLink != null) {
			sb.append(currentLink.toString() + '\n');
			currentLink = currentLink.next;
		}

		return sb.toString();
	}
}
